# -*- coding: utf-8 -*-
"""
    作者：juice
    日期：24/03/2020
    功能：解析表格   一行多列规则数据
    版本：5.0
"""
import csv

if __name__ == "__main__":
    path = 'JJF.csv'  # 默认目录
# Python自带库
f = open(path, 'r')
str001 = '''( '''
str002 = '''\''''
str003 = ''','''
str004 = ''' ), '''
str007 = ''' \r '''
str008 = ''''''
str005 = ''''''
str006 =''' INSERT INTO `quality_technical_basis`(`standard_code`, `seq_num`, `release_year`,`use_scope`,`chinese_name`, `english_name`,`release_date`,`implement_date`, `release_agency`, `china_standard_classify`,`global_standard_classify`) values '''
standard_code = ''''''#管理编号
release_year = ''''''#发布年号
seq_num = ''''''  #顺序号
use_scope = '''''' #适用范围
release_date = ''''''#发布日期
chinese_name = ''''''
english_name = ''''''
implement_date = ''''''#实施日期
release_agency = ''''''#发布单位
china_standard_classify = '''''' #中国标准分类
global_standard_classify = ''''''#国际标准分类
i =0
j =0
d = csv.reader(f)
# 逐行读入
for line in d:
    if len(line) > 0:
        str = line[0].strip();
        if str.startswith('''JJF'''):
            standard_code = str[0:3]
            str02 = str[3:].split('-');
            seq_num = str02[0].strip();
            release_year = str02[1].strip();
            i = i+1
        elif str.startswith('''【现行】'''):
            continue
        elif str.startswith('''【发布日期】'''):
            release_date = str[6:]
        elif str.startswith('''【实施日期】'''):
            implement_date = str[6:]
        elif str.startswith('''【发布单位】'''):
            release_agency = str[6:]
        elif str.startswith('''【中国标准分类】'''):
            china_standard_classify = str[8:]
        elif str.startswith('''【国际标准分类】'''):
            global_standard_classify = str[8:]
            j=j+1
        elif str.startswith('''【适用范围】'''):
            continue
        elif str.startswith('''本规程适用于'''):
            use_scope = str
        else:
            str04 = str.replace("TD-SCDMA","TDSCDMA").replace("Division-Synchronous","DivisionSynchronous")
            str03 = str04.split('-')
            chinese_name = str03[0].replace("TDSCDMA","TD-SCDMA").replace("DivisionSynchronous","Division-Synchronous")
            if len(str03) > 1:
                english_name = str03[1]
        if i == j:
            str005 += str001 + str002 + standard_code + str002 + str003 + str002 + seq_num + str002 + str003 + str002 + release_year + str002 + str003 + str002 + use_scope + str002 + str003 + str002 + chinese_name + str002 + str003 + str002 + english_name + str002 + str003 + str002 + release_date + str002 + str003 + str002 + implement_date + str002 + str003 + str002 + release_agency + str002 + str003 + str002 + china_standard_classify + str002 + str003 + str002 + global_standard_classify + str002 +str004+str007
f.close()
str005 = str005.replace("'s",'\\\'s')#替换's这种特殊字符;
str005 = str005.strip()[:-1]+''';''' #去除空格,将最后一位逗号换成;
wf = open("foo1.txt", "w")
wf.write(str006 +str007+ str005)
# 关闭打开的文件，必须关闭不然电脑能炸裂
wf.close()
